package com.whfc.ms.api.sys.service.impl;

import com.whfc.common.enums.SysRulePlatform;
import com.whfc.common.result.PageData;
import com.whfc.fuum.dto.SysDeptDTO;
import com.whfc.fuum.dto.SysDeptExtraDTO;
import com.whfc.fuum.dto.SysUserDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.param.*;
import com.whfc.fuum.service.AppProjectService;
import com.whfc.fuum.service.SysDeptService;
import com.whfc.fuum.service.SysUserService;
import com.whfc.ms.api.sys.service.MsSysDeptService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author qzexing
 * @version 1.0
 * @date 2020-12-09 17:58
 */
@Service
public class MsSysDeptServiceImpl implements MsSysDeptService {

    @DubboReference(interfaceClass = SysDeptService.class, version = "1.0.0")
    private SysDeptService deptService;

    @DubboReference(interfaceClass = AppProjectService.class, version = "1.0.0")
    private AppProjectService appProjectService;

    @DubboReference(interfaceClass = SysUserService.class, version = "1.0.0")
    private SysUserService sysUserService;

    @Override
    public List<SysDeptDTO> getDeptTree(SysUser sysUser, Integer deptId, String keyword) {
        return deptService.getDeptTree(sysUser, deptId, keyword);
    }

    @Override
    public void addDept(SysDeptAdd deptAdd) {
        deptService.addDept(deptAdd);
    }

    @Override
    public void editDept(SysDeptEdit deptEdit) {
        deptService.editDept(deptEdit);
    }

    @Override
    public void delDept(Integer deptId) {
        deptService.delDept(deptId);
    }

    @Override
    public PageData<SysDeptDTO> list(AppDeptListParam request) {
        return deptService.list(request);
    }

    @Override
    public List<SysDeptDTO> getDeptListByRuleCode(SysUser sysUser, Integer deptId, String ruleCode, SysRulePlatform platform) {
        return deptService.getDeptListByRuleCode(sysUser, deptId, ruleCode, platform);
    }

    @Override
    public void indexMove(SysDeptMoveIndex request) {
        deptService.indexMove(request);
    }

    @Override
    public void importUser(SysDeptUserImport request) {
        if (request.getEmpId() != null){
            if (request.getUserIds().size() > 1){
                throw new RuntimeException("只能导入一个用户");
            }
            SysUserDTO userDTO = sysUserService.queryUserById(request.getUserIds().get(0));
            if (userDTO == null) {
                throw new RuntimeException("用户不存在");
            }
            SysEmpUserAdd empUserAdd = new SysEmpUserAdd();
            SysDeptUserAdd sysDeptUser = new SysDeptUserAdd();
            BeanUtils.copyProperties(userDTO, sysDeptUser);
            sysDeptUser.setDeptId(request.getDeptId());
            empUserAdd.setAppEmpId(request.getEmpId());
            empUserAdd.setSysDeptUser(sysDeptUser);
            sysUserService.addEmpUser(empUserAdd);
        }
        deptService.importUser(request);
    }

    @Override
    public void removeUser(Integer deptId, Integer userId) {
        deptService.removeUser(deptId, userId);
    }

    @Override
    public PageData<SysUserDTO> getImportUserList(SysUser sysUser, Integer deptId, Integer pageNum, Integer pageSize, String keyword) {
        return deptService.getImportUserList(sysUser, deptId, pageNum, pageSize, keyword);
    }

    @Override
    public PageData<SysUserDTO> getImportEmpUserList(SysUser sysUser, Integer deptId, Integer pageNum, Integer pageSize, String keyword) {
        return deptService.getImportEmpUserList(sysUser, deptId, pageNum, pageSize, keyword);
    }

    @Override
    public SysDeptExtraDTO getDeptExtra(Integer deptId) {
        return deptService.getDeptExtra(deptId);
    }

    @Override
    public void setDeptExtra(SysDeptExtraParam param) {
        deptService.setDeptExtra(param);
    }

    @Override
    public String getExtraCustomizeField(Integer deptId) {
        return appProjectService.getProjectCustomizeField(deptId);
    }
}
